<?php

namespace App\Models\Mgt;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Laracasts\Presenter\PresentableTrait;

//use GeneaLabs\LaravelModelCaching\Traits\Cachable;

class User extends Authenticatable
{
    use Notifiable, HasRoles, PresentableTrait;

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $table = 'biz_users';

    protected $presenter = 'App\Http\Presenters\UserPresenter';

    //protected $guard_name = 'admin';

    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $guarded = array();

    protected $with = ['summary'];

    public $timestamps = false;

    const USER_TYPE_READER = 1;     //阅读者

    const USER_TYPE_VOLUNTEER = 2;  //志愿者

    public function branch()
    {
        return $this->belongsTo('App\Models\Mgt\Branch');
    }

    public function school()
    {
        return $this->belongsTo('App\Models\Mgt\School');
    }

    public function summary()
    {
        return $this->hasOne('App\Models\Biz\Summary', 'uid');
    }

    //签到记录
    public function tracks()
    {
        return $this->hasMany('App\Models\Biz\Track', 'uid', 'id')
                ->orderBy('date', 'DESC')
                ->with('activity');
    }

    //借阅记录
    public function borrows()
    {
        return $this->hasMany('App\Models\Biz\Borrow', 'uid', 'id')
                ->orderBy('date', 'DESC')
                ->with('book');
    }

    //已还书籍
    public function returns()
    {
        return $this->hasMany('App\Models\Biz\Borrow', 'uid', 'id')
                ->where('status', 1)
                ->orderBy('date', 'DESC')
                ->with('book');
    }

    //未还书籍
    public function overdue()
    {
        return $this->hasMany('App\Models\Biz\Borrow', 'uid', 'id')
                ->where('status', 0)
                ->orderBy('date', 'DESC')
                ->with('book');
    }

    //积分记录
    public function records()
    {
        return $this->hasMany('App\Models\Biz\Record', 'uid', 'id')
                ->orderBy('date', 'DESC')
                ->with('activity');
    }


    /*
    读者
     */
    public function scopeReader($query)
    {
        return $query->where('type', 1);
    }

    /*
    志愿者
     */
    public function scopeVolunteer($query)
    {
        return $query->where('type', 2);
    }

    /**
     * 培训报名记录
     * @return [type] [description]
     */

    public function terms()
    {
        return $this->hasManyThrough(
                'App\Models\Edu\Term',
                '\App\Models\Edu\Tuser',
                'uid', 'id', 'id', 'term_id')
            ->distinct();
    }


    public function clases()
    {
        return $this->hasMany('App\Models\Edu\Tuser', 'uid', 'id');
    }

    /**
     * 用户加入的班级花名册
     * @return [type] [description]
     */
    public function student()
    {
        return $this->hasMany('App\Models\Lst\Student','uid', 'id');
    }

    public function isJoin($publishId,$claseId)
    {
        return !!$this->student()->where('publish_id',$publishId)->where('clase_id',$claseId)->count();
    }
    /*
    常规活动是否签到
     */
    public function signed($aid, $type)
    {
        if ($type == 3 && $this->type == 2) return true;

        $track = $this->tracks->where('aid', $aid)
            ->where('uid', $this->id)
            ->where('type', $type)
            ->first();

        if ($track) return true;

        return false;
    }


    /*
    报名活动是否报名
     */
    public function applied($aid, $type)
    {
        $track = \App\Models\Biz\Track::where('aid', $aid)
            ->where('uid', $this->id)
            ->where('sign', 1)
            ->where('type', $type)
            ->first();

        if ($track) return true;

        return false;
    }


    /*
    报名活动是否签到
     */
    public function checked($aid)
    {
        $track = $this->tracks->where('aid', $aid)
            ->where('uid', $this->id)
            ->where('check', 1)
            ->first();

        if ($track) return true;

        return false;
    }


    /**
     * 培训课程是否已经报名
     * @param  [type] $pid [description]
     * @param  [type] $gid [description]
     * @return [type]      [description]
     */
    public function grade($pid, $gid)
    {
        $result = \App\Models\Edu\Enrol::where('publish_id', $pid)
            ->where('grade_id', $gid)
            ->where('uid', $this->id)
            ->first();

        if (!!$result)
            return true;

        return false;
    }


    public function setEnrol($request, $grade)
    {
        //额满限招班一旦报名就确认录取
        $status = 1;
        if ($grade->limited == 1)
            $status = 2;

        $enrol = new \App\Models\Edu\Enrol;

        $enrol->publish_id = $request->publish_id;
        $enrol->plan_id = $request->plan_id;
        $enrol->uid = $this->id;
        $enrol->account = $this->account;
        $enrol->category_id = $request->category_id;
        $enrol->grade_id = $request->grade_id;
        $enrol->status = $status;
        $enrol->source = $request->source;
        //dd($grade->name);
        return $enrol->save();
    }


    public function books()
    {
        return $this->hasMany('\App\Models\Mgt\Book','donation_uid');
    }


    public static function checkAge($birthday)
    {
        if (strlen($birthday) != 8)
            return false;

        $byear = substr($birthday, 0, 4);
        $bmonth = substr($birthday, 4, 2);
        $bday = substr($birthday, 6, 2);

        //格式化当前时间年月日
        $tyear = date('Y');
        $tmonth = date('m');
        $tday = date('d');


        $age = $tyear - $byear;
        if ($bmonth > $tmonth || $bmonth == $tmonth && $bday > $tday) {
            $age--;
        }

        if ($age <= 16)
            return true;

        return false;
    }

    /**
     * [年龄检测]
     * @param  [type]  $birthday [description]
     * @param integer $valid [description]
     * @return [type]            [description]
     */
    public static function checkBirthday($birthday, $valid = 80)
    {
        if (strlen($birthday) != 8)
            return false;

        $y = substr($birthday, 0, 4);
        $m = substr($birthday, 4, 2);
        $d = substr($birthday, 6, 2);
        $year = date('Y');

        if (!checkdate($m, $d, $y)) {
            return false;
        } else {
            if (($year - $y) < 0)
                return false;
            if (($year - $y) > $valid)
                return false;
        }

        return true;
    }


    public static function checkMobile($mobile, $type)
    {
        $row = self::where('mobile', $mobile)->where('type', $type)->get();

        if (count($row) > 0) return true;

        return false;
    }


    public static function checkIdcard($idcard, $type)
    {
        $row = self::where('idcard', $idcard)->where('type', $type)->get();

        if (count($row) > 0) return true;

        return false;
    }
}
